<%-- 
    Document   : test
    Created on : Sep 9, 2011, 4:29:14 AM
    Author     : northpole
--%>

<%@page import="java.util.ArrayList"%>
<%@page import="user.User" contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <link type="text/css" rel="stylesheet" href="sidebar.css" media="screen" />

        <title>Profile Page</title>
        <%User userInfo = null;
                    userInfo = (User) session.getAttribute("userInfo");
                    int privMode = -1;
                    User stranger = new User();
                    User friend = new User();
                    ArrayList friends = null;
                    String username = null, name = null, surname = null, date = null, welcomeText = null;
                    String[] interests = null;
                    String requestUname = request.getParameter("username");

                    if (session == null || session.getAttribute("username") == null) {
                        response.sendRedirect("ErrorPage.html");
                        return;

                    }


                    if (requestUname == null) {
                        requestUname = "nulll";
                        response.sendRedirect("ErrorPage.html");
                        return;
                    }

                    // out.print("Welcome afadsf"+ request.getParameter("username"));
               //    try {
                        if (requestUname.equals(session.getAttribute("username"))) {//re-visit own profile
                            out.print("Welcome " + session.getAttribute("username") + "");
                            userInfo = (User) session.getAttribute("userInfo");

                            privMode = 1;

                            //out.println("id " + session.getAttribute("id") + " </br>");
                            name = userInfo.getName();
                            surname = userInfo.getSurname();
                            interests = userInfo.getInterests().split(",");
                            username = userInfo.getUsername();
                            
                            //        welcomeText=session.getAttribute("welcomeText").toString();
                            date = userInfo.getDateOfBirth();

                            userInfo.loadFriendList();
                            friends = (ArrayList) session.getAttribute("friends");
                            friends = userInfo.getFriendUsernames();


                        } else if (!requestUname.equals(session.getAttribute("username"))) {//visit not own profile
                            if (userInfo != null&&userInfo.isLogged() && userInfo.isFriend(request.getParameter("username")))
                                {//visiting a friend
                                privMode = 2;
                                friend.setUsername(request.getParameter("username"));
                                friend.loadFriendInfo();
                                ArrayList inf = friend.getFriendInfo();
                                if(inf!=null &&!inf.isEmpty())
                                if (inf.size() == 5) {
                                    if(inf.get(0)!=null)
                                    name = inf.get(0).toString();
                                    if(inf.get(1)!=null)
                                    surname = inf.get(1).toString();
                                    if(inf.get(2)!=null)
                                    date = inf.get(2).toString();
                                    if(inf.get(3)!=null)
                                    interests = inf.get(3).toString().split(",");
                                    if(inf.get(4)!=null)
                                    welcomeText=inf.get(4).toString();
                                }
                            } else if ((userInfo != null && !userInfo.isFriend(request.getParameter("username")))) {
                                //visit a stranger
                                privMode = 3;
                                if (stranger.getMisc().usernameExists(request.getParameter("username"))) {
                                    stranger.setUsername(request.getParameter("username"));
                                    stranger.loadPublicInfo();
                                    ArrayList inf = stranger.getPublicInfo();
                                    if(inf!=null &&!inf.isEmpty())
                                     if(inf.get(0)!=null)
                                        interests = inf.get(0).toString().split(",");
                                    if(inf.get(1)!=null)
                                      welcomeText=inf.get(1).toString();
                                } else {
                                    out.print("Non Existent Username");
                                }
                            }
                            else if(userInfo==null)//no session means no login
                                {privMode=3;//sinve there is not any essential diff
                                 if (stranger.getMisc().usernameExists(request.getParameter("username"))) {
                                    stranger.setUsername(request.getParameter("username"));
                                    stranger.loadPublicInfo();
                                    ArrayList inf = stranger.getPublicInfo();
                                    if(inf!=null &&!inf.isEmpty())
                                     if(inf.get(1)!=null)
                                        interests = inf.get(1).toString().split(",");
                                    if(inf.get(0)!=null)
                                      welcomeText=inf.get(0).toString();
                                } else {
                                    out.print("Non Existent Username");
                                }
                            
                            }
                        }
        %>
    </head>
    <body>
    
        <div id ="footbar">
            <div id="RightBar">
                <br>
                <a href="endSession.jsp"><b>
                        <input type="image" src="imadge/on-off.jpg" name="image" background-color="#C0C0C0" width="35" height="35" >
                    </b>
                </a>
            </div>
        </div>
        <div id="rightbar" class="sidebox">

            <table id="right">
                <tr> <td>
                        <hr>
                        <ul>

                            <%
                                        if (privMode == 1) {
                                            if(userInfo!=null)
                                        if(userInfo.pendingFriendRequestsTo()!=null)
                                            if(!userInfo.pendingFriendRequestsTo().isEmpty())
                                             if(userInfo.pendingFriendRequestsTo().size()>0)
                                              {out.print(userInfo.pendingFriendRequestsTo().size()+" users want to be your friends");
                                             for(int i=0;i<userInfo.pendingFriendRequestsTo().size();i++)
                                                out.print("<a href=\"acceptFriend.jsp?username="
                                                        +userInfo.pendingFriendRequestsTo().get(i)+"Accept</a> "
                                                      +userInfo.pendingFriendRequestsTo().get(i));

                                                 }
                                            if (friends != null && friends.size() > 1) {
                                                out.println("Your Friends are " + friends.size() + "</br> ");



                                                for (int i = 0; i < friends.size(); i++) {
                            %>

                            <li><b>

                                    <%out.println("<a href=profile.jsp?username=" + friends.get(i) + ">" + friends.get(i) + "</a></br>");
                                                            }
                                                        }
                                                        //   if(userInfo.pendingRequestsFromUser().length()>1)
                                                        //         {for(int i=0;i<userInfo.pendingRequestsFromUser().length();i++)
                                                        //   out.print("<a href=profile.jsp?username="+userInfo.pendingRequestsFromUser().get(i)+">"+"</a>");
                                                        //              out.print("These users are not yet sure they want to be your friends");}
%>

                                </b></li>

                            <%

                                                String[] suggestions = userInfo.SuggestFriends();
                                                if (suggestions != null)
                                                    if (suggestions.length >= 1)
                            %><li><% out.print("You should be friends with</br>");
                                %></li>><%
                                                    for (int i = 0; i < suggestions.length; i++) {
                                %>

                            <li><b>
                                    <%out.print("<a href=profile.jsp?username=" + suggestions[i] + ">" + suggestions[i] + "</a></br>");
                                                        }


                                    %>
                                    <a href="manageFriends.jsp">Manage Your Friends</a>
                                </b></li>
                                <%
                                            }

                                %>
                        </ul>
                        <hr>
                        comon-popular <br>
                        interests list
                        <hr>
                    </td>
                </tr>
            </table>





        </div>
        <div id ="headbar">
            <%if (privMode == 1) {%>
            <a href="profile.jsp?username=<%out.print(userInfo.getUsername());%>">
                <h1>Abandonware <br/> Place</h1>  </a>>
                    <%} else{
                    %><h1>Abandonware <br/> Place</h1>
                    <%}%>
            <div id ="searchbar">
                <form action="search.jsp" method="GET">
                    <input type="text" name="username" placeholder="search for usernames"/>
                    <a href="search.jsp"> Advanced Search</a>
                </form></div>
        </div>

        <div id="leftbar" class="sidebox">
                <%if(userInfo!=null){%>
         <div style="z-index: 100%;">
             <a href="profile.jsp?username=<%out.print(session.getAttribute("username"));%>" >Go Back
<img src="imadge/pixel-mario.gif" alt="HTML tutorial" width="32" height="32" />
</a>
        </div>
        <%}else{%> <div style="z-index:100%;">
        <a href="index.jsp" >Go Back
<img src="imadge/pixel-mario.gif" alt="HTML tutorial" width="32" height="32" />
</a>
        </div>
        <%}%>
            <table style="width:5%;">
                <tr> <td>                      
                        <hr>
                        <% 
                                    if (privMode == 1) {
                                        out.println("</br>Info:</br>");
                                    } else if (privMode == 2 || privMode == 3) {
                                        out.print("</br></br>Username:</br><h3>" + request.getParameter("username") + "</h3></br>");
                                    }

                                    if (name != null) {
                                        out.println("NAME </br>" + name);
                                    }

                                    if (surname != null) {
                                        out.println(surname + "</br></br>");
                                    }
                                    //if(interests!=null)
                                    //   out.println(interests);
                                    if (username != null) {
                                        out.println("USERNAME</br>" + username + "</br></br>");
                                    }
                                    // if(welcomeText!=null)
                                    //   out.println(welcomeText);
                                    if (date != null) {
                                        out.println("You were born at " + date);
                                    }


                        %>

                        <!--               <b><input type="image" src="imadge/Pixel_Character_6__Yoshi_by_meowmixkitty1.jpg" name="image" width="55" height="55" >
                                  </b>
                        -->
                        <%if (privMode == 1) {%>
                        <a href="Pms.jsp">
                            <b>Edit</b>
                        </a>

                        <hr />
                        <% ArrayList temp = userInfo.getUnreadPms();
            if (!temp.isEmpty()) {
                out.println("<a href=readPms.jsp> You have " + temp.size() + "UnreadPms </a>");%>
                        <a href="readPms.jsp">
                            <b><input type="image" src="imadge/pms.jpg" name="pms" background-color="#C0C0C0" width="35" height="35" >
                            </b>
                        </a>
                        <hr />
                        <%}
                        %>            <a href="Pms.jsp">Sent A new Pm</a><br>
                        <% }%>

                        <%
                                    if (privMode == 1) {
                                        out.print("Interests (" + interests.length + ")<ul>");

                                        for (int i = 0; i < interests.length; i++) {
                                            out.print("<li>" + interests[i] + "</li>");
                                        }
                                        out.print("</ul>");
                                    }
                        %>
                        <hr />
                    </td>


                </tr>
            </table>

        </div>

        <div id="mainContainer">
             <%           if(userInfo!=null&&userInfo.getSuggestions()!=null && !userInfo.getSuggestions().isEmpty())
                         {out.print(userInfo.getSuggestions().toString());}
             %>

    </div>


    <a href="profile.jsp?username=northy">visit northy</a>
    <div id="text">
        <%if (privMode == 1) {%>
        
        <iframe src="http://wterminal.appspot.com/demo" style="position:relative;left:-40px;top: -20px; width:930px; height:480px;">
            <p>Your browser does not support iframes.</p>
        </iframe>
        <%}
                 else if (privMode == 3) {
                        out.print("<h1>Hello and Welcome to " + stranger.getUsername() + " 's Profile!!!</h1></br>");
                        out.print("<h2> Here is what " + stranger.getUsername() + " has to say to you!!!</h2></br>");
                        out.print(welcomeText+ "</br></br>");

                        out.print("<h2> Here is " + stranger.getUsername() + " 's Interests </h2><ul>");
                        
                        if(interests!=null){
                        for (int i = 0; i < interests.length; i++) {
                            out.print("<li>" + interests[i] + "</li>");
                        }}
                        else
                            out.print("The user enjoys Breathing,eating,sleeping and Generally Being Somewhat alive even though"
                                    + " we doubt that,<br> cuz a user without interests is either a 4chan user or a zombie");
                        out.print("</ul>");

                    } else if (privMode == 2) {
                        out.print("<h1>Hello and Welcome to " + friend.getUsername() + " 's Profile!!!</h1></br>");
                        
                        if(friend.getWelcomeText()!=null&&!friend.getWelcomeText().isEmpty())
                        {out.print(" <h2>Here is what " + friend.getUsername() + " has to say to you!!!</h2></br>");
                        out.print(welcomeText + "</br></br>");
                       } else
                        {out.print(friend.getUsername()+" Has nothing to share with you");}
                      out.print("<h2> Here is " + friend.getUsername() + " 's Interests </h2><ul>");

                        if(interests!=null){
                        for (int i = 0; i < interests.length; i++) {
                            out.print("<li>" + interests[i] + "</li>");
                        }}
                        else
                            out.print("The user enjoys Breathing,eating,sleeping and Generally Being Somewhat alive even though"
                                    + "\n we doubt that,<br> cuz a user without interests is either a 4chan user or a zombie");
                        out.print("</ul>");


                    }
        %>

        <%if (privMode == 3 &&userInfo!=null) {
                        out.print("</br>Do you whant to make " + stranger.getUsername() + " your friend?</br>");
        %>
        <a href="addFriend.jsp?username=<%out.print(request.getParameter("username"));%> ">clik me!!</a>
        <%}

                 //   } catch (java.lang.NullPointerException e) {
                   //    out.println("error"); response.sendRedirect("ErrorPage.html");return;
                   // }
        %>
    </div>

    <%out.print("PRIVMODE=="+privMode); %>
</body>
</html>
